package com.iafme.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.MessageReqDTO;
import com.iafme.dto.res.MessageResDTO;
import com.iafme.model.Message;
import com.iafme.service.MessageService;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/19 09:49
 */
@RestController
@RequestMapping(value = "/message")
@AllArgsConstructor
public class MessageController implements BaseCommonInterface<MessageReqDTO, MessageResDTO> {

    private final MessageService messageService;

    @OperateAnnotation(businessType = BusinessType.INSERT,name = "消息中心")
    @SaCheckPermission("website:message:add")
    @Override
    public Result<String> save(@Valid MessageReqDTO messageReqDTO) {
        messageService.saveMessage(messageReqDTO);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "消息中心")
    @SaCheckPermission("website:message:edit")
    @Override
    public Result<String> update(@Valid MessageReqDTO messageReqDTO) {
        messageService.updateMessage(messageReqDTO);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.DELETE,name = "消息中心")
    @SaCheckPermission("website:message:delete")
    @Override
    public Result<String> del(Integer id) {
        messageService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<MessageResDTO>> lists(MessageReqDTO messageReqDTO) {
        return Result.successResult(messageService.lists(messageReqDTO));
    }

    @Override
    public Result<MessageResDTO> findById(Integer id) {
        return Result.successResult(messageService.findById(id));
    }

    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "消息中心")
    @SaCheckPermission("website:message:delete")
    @Override
    public Result<String> batchDel(List<MessageResDTO> list) {
        List<Integer> ids = list.stream().map(MessageResDTO::getId).collect(Collectors.toList());
        messageService.removeByIds(ids);
        return Result.successResult();
    }
}
